package com.inspur.myapplication.dao;

import android.content.Context;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Administrator on 2020/12/3.
 */

public class CartsDao {
    private Context context;

    public CartsDao(Context context){
        this.context=context;
    }

    public ArrayList queryCarts(String userName){
        ArrayList arrayList=new ArrayList();

        HashMap hashMap=new HashMap();
        hashMap.put("goodsName","goods1");
        hashMap.put("goodsPrice","100");
        hashMap.put("goodsImg","https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1541231763,2897362241&fm=26&gp=0.jpg");
        hashMap.put("quantity","1");
        hashMap.put("goodsId","123");
        hashMap.put("sel","0");
        arrayList.add(hashMap);

        hashMap=new HashMap();
        hashMap.put("goodsName","goods2");
        hashMap.put("goodsPrice","110");
        hashMap.put("goodsImg","https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1541231763,2897362241&fm=26&gp=0.jpg");
        hashMap.put("quantity","2");
        hashMap.put("goodsId","124");
        hashMap.put("sel","0");
        arrayList.add(hashMap);

        hashMap=new HashMap();
        hashMap.put("goodsName","goods3");
        hashMap.put("goodsPrice","120");
        hashMap.put("goodsImg","https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1541231763,2897362241&fm=26&gp=0.jpg");
        hashMap.put("quantity","3");
        hashMap.put("goodsId","125");
        hashMap.put("sel","0");
        arrayList.add(hashMap);

        return arrayList;
    }
    //计算选择商品的总金额
    public float calMoney(ArrayList arrayList){
        float result=0;

        for(int i=0;i<arrayList.size();i++){
            HashMap hashMap=(HashMap) arrayList.get(i);
            if(hashMap.get("sel").toString().equals("1")){
                float quantity=Float.parseFloat(hashMap.get("quantity").toString());

                float goodsPrice=Float.parseFloat(hashMap.get("goodsPrice").toString());

                result+=goodsPrice*quantity;
            }
        }

        return result;
    }

    public HashMap getOneCartDataById(String goodsId,ArrayList arrayList){
        HashMap hashMap=null;
        for(int i=0;i<arrayList.size();i++) {
            if(((HashMap) arrayList.get(i)).get("goodsId").equals(goodsId)){
                hashMap=(HashMap) arrayList.get(i);
                break;
            }
        }
        return hashMap;
    }

    public void checkAll(ArrayList arrayList,boolean b){
        for(int i=0;i<arrayList.size();i++) {
            if(b){
                ((HashMap) arrayList.get(i)).put("sel","1");
            }else {
                ((HashMap) arrayList.get(i)).put("sel","0");
            }
        }
    }
}
